﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.ContentModel;
using Newtonsoft.Json;
using Proggy.Web.Cms.Core.PropertyTypes;
using Proggy.Web.Cms.Core.Configuration.ConfigObjects;
using Proggy.Web.Cms.Core.Seo.Service.Entities;
using Proggy.Web.Cms.Cache;
using Proggy.Web.Cms.Context;
using System.Web;
using System.Configuration;
using System.Web.Mvc;
using Proggy.Web.Cms.Repositories.ContentModel;
using Proggy.Web.Cms.Repositories.Published;
using Proggy.Web.Cms.Repositories.Membership;

namespace Proggy.Web.Cms.Core.ContentModels
{
    public abstract class ProggyCoreAddressableContentModelBase : ProggyCoreContentModelBase, IContentModel
    {
        public ProggyCoreAddressableContentModelBase(IContentModelTypeRepository cmRepo, IMembershipRepository membRepo) : base(cmRepo, membRepo) { }
        public ProggyCoreAddressableContentModelBase() : base() { }

        /// <summary>
        /// The description for seo purposes
        /// </summary>
        [ContentModelPropertyType("Seo", "Seo description (150 characters max)", "SeoDescription")]
        public CharLimitedTextString SeoDescription { get; set; }

        /// <summary>
        /// The seo description config object
        /// </summary>
        [JsonIgnore]
        public CharLimitedTextStringConfigObject SeoDescriptionConfigObject
        {
            get
            {
                return new CharLimitedTextStringConfigObject
                {
                    Limit = 150,
                    Minimum = 50
                };
            }
        }

        /// <summary>
        /// Ignores
        /// </summary>
        [ContentModelPropertyType("Seo", "Ignore in sitemap", "IgnoreInSiteMap")]
        public TrueOrFalse IgnoreInSiteMap { get; set; }

        /// <summary>
        /// Ignores
        /// </summary>
        [ContentModelPropertyType("Seo", "Disallow robots", "DisallowRobots")]
        public TrueOrFalse DisallowRobots { get; set; }

        /// <summary>
        /// Content change frequency for site map generation
        /// </summary>
        [JsonIgnore]
        public abstract SiteMapChangeFrequencyEnum SiteMapChangeFrequency { get; }

        /// <summary>
        /// Content priority for site map generation
        /// </summary>
        [JsonIgnore]
        public abstract SiteMapPriorityEnum SiteMapPriority { get; }

        /// <summary>
        /// If true, content will appear on site map, and children will appear (if their ContentModel property is also "true")
        /// </summary>
        [JsonIgnore]
        public abstract bool ContentShouldAppearOnSiteMap { get; }

        /// <summary>
        /// Update site map
        /// </summary>
        /// <param name="repo"></param>
        public override void OnAfterSuccessfulPublish(Service.IContentCrudService repo)
        {
            try
            {
                // get service
                ProggyCoreManager.Instance.UpdateSiteMap();
                ProggyCoreManager.Instance.GetIRobotsFileInstance().GenerateRobotsFile();
            }
            catch
            {
                //
            }



            // continue
            base.OnAfterSuccessfulPublish(repo);
        }

        /// <summary>
        /// Update site map
        /// </summary>
        /// <param name="repo"></param>
        public override void OnAfterSuccessfulMove(Service.IContentCrudService repo)
        {
            try
            {
                // get service
                ProggyCoreManager.Instance.UpdateSiteMap();
            }
            catch
            {
                //
            }

            base.OnAfterSuccessfulMove(repo);
        }

        /// <summary>
        /// Update site map
        /// </summary>
        /// <param name="repo"></param>
        public override void OnAfterSuccessfulSort(Service.IContentCrudService repo)
        {
            try
            {
                // get service
                ProggyCoreManager.Instance.UpdateSiteMap();
            }
            catch
            {
                //
            }

            base.OnAfterSuccessfulSort(repo);
        }

        /// <summary>
        /// Update site map
        /// </summary>
        /// <param name="repo"></param>
        public override void OnAfterSuccessfulTrash(Service.IContentCrudService repo)
        {
            try
            {
                // get service
                ProggyCoreManager.Instance.UpdateSiteMap();
            }
            catch
            {
                //
            }

            base.OnAfterSuccessfulTrash(repo);
        }

        //[ContentModelPropertyType("Seo", "Include this content in the xml sitemap?", "IncludeInSeoSiteMap")]
        //public TrueOrFalse IncludeInSeoSiteMap
        //{
        //    get
        //    {
        //        return this._includeInSeoSiteMap;
        //    }
        //    set
        //    {
        //        this._includeInSeoSiteMap = value;
        //    }
        //}
        //private TrueOrFalse _includeInSeoSiteMap = new TrueOrFalse { SavedValue = true };

        ///// <summary>
        ///// Site map frequency
        ///// </summary>
        //[ContentModelPropertyType("Seo", "Content change frequency", "SiteMapFrequency")]
        //public DropDown SiteMapFrequency { get; set; }

        //[JsonIgnore]
        //public DropDownConfigObject SiteMapFrequencyConfigObject
        //{
        //    get
        //    {
        //        return new DropDownConfigObject
        //        {
        //            Data = new List<KeyValuePair<string, string>> { 
        //                new KeyValuePair<string,string>("daily","daily"),
        //                new KeyValuePair<string,string>("weekly","weekly"),
        //                new KeyValuePair<string,string>("monthly","monthly"),
        //                new KeyValuePair<string,string>("hourly","hourly"),
        //                new KeyValuePair<string,string>("always","always"),
        //                new KeyValuePair<string,string>("yearly","yearly"),
        //                new KeyValuePair<string,string>("never","never")
        //            }
        //        };
        //    }
        //}

        ///// <summary>
        ///// Site map frequency
        ///// </summary>
        //[ContentModelPropertyType("Seo", "Content priority in site", "SiteMapPriority")]
        //public DropDown SiteMapPriority { get; set; }

        //[JsonIgnore]
        //public DropDownConfigObject SiteMapPriorityConfigObject
        //{
        //    get
        //    {
        //        return new DropDownConfigObject
        //        {
        //            Data = new List<KeyValuePair<string, string>> { 
        //                new KeyValuePair<string,string>("0.5","0.5"),
        //                new KeyValuePair<string,string>("0.6","0.6"),
        //                new KeyValuePair<string,string>("0.7","0.7"),
        //                new KeyValuePair<string,string>("0.8","0.8"),
        //                new KeyValuePair<string,string>("0.9","0.9"),
        //                new KeyValuePair<string,string>("0.4","0.4"),
        //                new KeyValuePair<string,string>("0.3","0.3"),
        //                new KeyValuePair<string,string>("0.2","0.2"),
        //                new KeyValuePair<string,string>("0.1","0.1")
        //            }
        //        };
        //    }
        //}        
    }
}
